package com.chance.cc.crawler.prod.command.job.domain.vm.dcdapp.trace;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.dcdapp.DCDAppCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @ClassName song
 * @Description TODO
 * @Author ding
 * @Date 2021/9/15 14:35
 * @Version 1.0
 * 动态板块回溯7天采集
 **/
public class DcdAppDynamicTraceCrawlerScheduleTrigger extends DCDAppCommonCrawlerSchedulerJob {

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }
    public static CrawlerJob publishCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = crawlerSchedulejob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("懂车帝口碑每天定时1:10回溯7天");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;
    }
    public static CrawlerJob crawlerSchedulejob(){
        String url = "https://www.dongchedi.com/";
        String site = "dynamic";
        String siteBiz = "everyday-trace";
        String site_info = "dongtai-everyday";
        String sycSite = "dongtai-everyday";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(dateRange)
//                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,crawler_level,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 7,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);

        //创建 评论信息的过滤条件
        CrawlerRecord filterCrawlerRecord =  new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        //以json转换保证数据传输的完整性
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));


        //获取地址的时候需要添加site确定拿取到的地址为本次需要的
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,siteBiz,"queue");
        CrawlerRequestRecord initCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dcd_dynamic_trace_item_request",turnPageItem)
                .httpUrl("http://"+metaServiceIp+":"+metaServicePort+"/crawler/domain/common/api/v1/"+domain+"/results/queue/init?requestQueueName="+requestQueueName + "&site="+sycSite+"&hourFromNow=170")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(crawlerRequestRecord), "utf-8");
        initCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//dynamic-everyday-trace-dongtai-everyday-realtime
                        StringUtils.joinWith("-",site,siteBiz,site_info,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .consoleResultPipeline("kafka")
                .fileResultPipeline("kafka", "/data/chance_crawler_runner/logs/node/dcdapp/dynamic.log", false)
                .kafkaResultPipeline("kafka",kafkaTopicForTraceJob,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(initCrawlerRecord)
                .build();
    }

}
